package com.m.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.m.system.domain.SysUserEntity;
import com.m.system.domain.UserRoleDomain;
import com.m.system.mapper.SysUserMapper;
import com.m.system.mapper.SysUserRoleMapper;
import com.m.system.service.SysUserService;
import com.m.utils.IdUtil;
import com.m.utils.UserUtil;

import org.springframework.beans.factory.annotation.Autowired;

/**
 * 用户信息表Service
 * 
 * @author monxz
 * @version 2021-01-29
 */
@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private SysUserMapper sysUserMapper;

	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	public SysUserEntity findSysUserById(SysUserEntity sysUserEntity) throws Exception {
		return sysUserMapper.get(sysUserEntity);
	}

	@Override
	public List<SysUserEntity> findSysUserList(SysUserEntity sysUserPojo) throws Exception {
		return sysUserMapper.getList(sysUserPojo);
	}

	@Override
	public Integer addSysUser(SysUserEntity sysUserPojo) throws Exception {
		// 新增用户
		sysUserPojo.setUserId(IdUtil.genId());
		sysUserPojo.setCreateBy(UserUtil.getUserName(null));
		List<String> roleIds = sysUserPojo.getRoleIds();
		if (roleIds != null && !roleIds.isEmpty()) {
			// 新增用户角色
			List<UserRoleDomain> urList = new ArrayList<UserRoleDomain>();
			for (String roleId : roleIds) {
				urList.add(new UserRoleDomain(sysUserPojo.getUserId(), roleId));
			}
			sysUserRoleMapper.addUserRoleList(urList);
		}
		return sysUserMapper.insert(sysUserPojo);
	}

	@Override
	public Integer modifySysUser(SysUserEntity sysUserPojo) throws Exception {
		sysUserRoleMapper.deleteByUserId(sysUserPojo.getUserId());
		if (sysUserPojo.getRoleIds() != null && !sysUserPojo.getRoleIds().isEmpty()) {
			List<UserRoleDomain> urList = new ArrayList<UserRoleDomain>();
			for (String roleId : sysUserPojo.getRoleIds()) {
				// 新增用户角色
				urList.add(new UserRoleDomain(sysUserPojo.getUserId(), roleId));
			}
			sysUserRoleMapper.addUserRoleList(urList);
		}
		return sysUserMapper.update(sysUserPojo);
	}

	@Override
	public Integer delete(SysUserEntity sysUserPojo) throws Exception {
		return sysUserMapper.delete(sysUserPojo);
	}

	@Override
	public Integer deleteFalse(String[] ids) throws Exception {
		return sysUserMapper.deleteFalse(ids);
	}

	@Override
	public Integer checkUnion(SysUserEntity sysUserPojo) throws Exception {
		List<SysUserEntity> sysUserList = sysUserMapper.getExctList(sysUserPojo);
		if (sysUserList == null || sysUserList.isEmpty()) {
			return 0;
		}

		if(sysUserPojo.getUserId() == null && sysUserList.size() > 0) {
			return 1;
		}
		
		if(!sysUserPojo.getUserId().equals(sysUserList.get(0).getUserId()) ) {
			return 1;
		}

		return 1;

	}

	@Override
	public Integer restPwd(String userId, String newpwd,String oldPwd) throws Exception {
		if(oldPwd == null) {
			SysUserEntity user = new SysUserEntity();
			user.setUserId(userId);
			user.setPassword(newpwd);
			return sysUserMapper.update(user);
		}
		SysUserEntity user = new SysUserEntity();
		user.setUserId(userId);
		user.setPassword(oldPwd);
		List<SysUserEntity>  userList = sysUserMapper.getExctList(user);
		if(userList == null || userList.isEmpty()) {
			return 0 ;
		}
		user.setPassword(newpwd);		
		return sysUserMapper.update(user);
	}
}